package basic;

// This file contains material supporting section 3.7 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com 

import java.io.*;
import java.sql.SQLException;

import com.sun.rowset.CachedRowSetImpl;

import client.*;
import common.*;

import flexibility.userData;

/**
 * This class constructs the UI for a chat client. It implements the chat
 * interface in order to activate the display() method. Warning: Some of the
 * code here is cloned in ServerConsole
 * 
 * @author Fran&ccedil;ois B&eacute;langer
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;re
 * @version July 2000
 */
public class ClientConsole implements ChatIF {
	// Class variables *************************************************
	public CachedRowSetImpl crs; // crs will hold the query results
	private  int hasChanged = 0;//hasChanged will changed to 1  once crs has the results of the query
	private boolean isSuccess = true;
	
	/**
	 * The default port to connect on.
	 */
	final public static int DEFAULT_PORT = 5555;

	// Instance variables **********************************************

	/**
	 * The instance of the client that created this ConsoleChat.
	 */
	ChatClient client;

	// Constructors ****************************************************

	/**
	 * Constructs an instance of the ClientConsole UI.
	 * 
	 * @param host
	 *            The host to connect to.
	 * @param port
	 *            The port to connect on.
	 */
	public ClientConsole(String host, int port) {
		try {
			// host = "localhost";
			// port = DEFAULT_PORT;
			client = new ChatClient(host, port, this);
		} catch (IOException exception) {
			System.out.println("Error: Can't setup connection!"
					+ " Terminating client.");
			isSuccess = false;
			//System.exit(1);
		}
	}
	
	public boolean getStatus(){
		//returns the server status (of connection)
		return isSuccess;
	}
	
	
	// Instance methods ************************************************

	/**
	 * This method waits for input from the console. Once it is received, it
	 * sends it to the client's message handler.
	 */
	/*
	 * public void accept() { try { BufferedReader fromConsole = new
	 * BufferedReader( new InputStreamReader(System.in)); ArrayList<Object>
	 * message;
	 * 
	 * 
	 * while (true) { message = fromConsole.readLine();
	 * client.handleMessageFromClientUI(message); } } catch (Exception ex) {
	 * System.out.println("Unexpected error while reading from console!"); } }
	 */

	public void acceptMsg(userData parameterList) {
		if (Integer.parseInt(parameterList.getData(0).toString()) == -1) {
			try {
				client.closeConnection();
				client.quit();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			client.handleMessageFromClientUI(parameterList);
		}

	}

	/**
	 * This method overrides the method in the ChatIF interface. It displays a
	 * message onto the screen.
	 * 
	 * @param message
	 *            The string to be displayed.
	 */
	public void display(Object message) {

		try {
			crs = new CachedRowSetImpl();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.getErrorCode();
		}
		if(message==null){
			hasChanged = 1;
		}
		crs = (CachedRowSetImpl) message; //crs receives the results of the query here
		hasChanged = 1;//hasChanged changes to 1 because crs has the results now
	}
	


	public CachedRowSetImpl getResults() {
		while(hasChanged==0){//this while loop run as long as crs has not yet got the results of the query
			}//this will avoid IO exception in the reading method of the client, because receiving the  query results
		hasChanged=0;
		return crs;// might take some time, depending on the network load
	}
	// Class methods ***************************************************

	/**
	 * This method is responsible for the creation of the Client UI.
	 * 
	 * @param args
	 *            [0] The host to connect to.
	 */

}

// End of ConsoleChat class
